package com.wancaijinfu.widget;

import com.wancaijinfu.actitvity.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2015/9/15.
 */
public class SmallCircleView extends View implements Runnable {

	private Paint circle, paint, textpaint;
	private Context content;
	private float radus = 40f;// 
	private float paint_width = 5f;// 
	private float angle = 0f;// 
	private float corner;// 
	private float textSize = 20f;// 
	private float number = 80f;// 
	private String text_angle = "正在投标";// 
	private int inCircle;
	private int outCircle = Color.BLUE;

	public void setProcess(int i,String text_angle) {
	
		angle=0f;
		this.number = i;
		
		corner = number / 100f * 360;
		angle=corner;
		this.text_angle=text_angle;
		postInvalidate();
		
	}

	public SmallCircleView(Context context) {

		this(context, null);
	}

	public SmallCircleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.content = content;
		init(context, attrs);
	}

	private void init(Context context, AttributeSet attrs) {
		TypedArray types = context.obtainStyledAttributes(attrs,
				R.styleable.mycircle);
		final int count = types.getIndexCount();
		for (int i = 0; i < count; i++) {
			int attr = types.getIndex(i);
			switch (attr) {
			case R.styleable.mycircle_incircle:
				inCircle = types.getColor(attr, Color.RED);
				break;
			case R.styleable.mycircle_outcircle:
				outCircle = types.getColor(attr, Color.RED);
				break;
			case R.styleable.mycircle_textsize:
				textSize=DipToPixels(context,(int)types.getFloat(attr, 20f))*2/3;
			
				break;
			case R.styleable.mycircle_circlewidth:
				paint_width = DipToPixels(context,(int)types.getFloat(attr, 5f))*2/3;
			
				break;
			case R.styleable.mycircle_circleradus:
				radus = DipToPixels(context,(int)types.getFloat(attr, 40f))*2/3;
				break;
			case R.styleable.mycircle_number:
				number = DipToPixels(context,(int)types.getFloat(attr, 10f))*2/3;
				
				break;
			}
		}
		types.recycle();

	}
	
	public int DipToPixels(Context context, int dip) {
        final float SCALE = context.getResources().getDisplayMetrics().density;

        float valueDips = dip;
        int valuePixels = (int) (valueDips * SCALE + 0.5f);

        return valuePixels;

    }
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension((int) ((radus + paint_width) * 2),
				(int) ((radus + paint_width) * 2));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		circle = new Paint();
		circle.setAntiAlias(true);
		circle.setStrokeWidth(paint_width);
		circle.setStyle(Paint.Style.STROKE);
		circle.setColor(inCircle);
		canvas.drawCircle(paint_width + radus, paint_width + radus, radus,
				circle);
		// super.onDraw(canvas);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(paint_width);
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(outCircle);
		// 锟斤拷圆
		// paint.setColor(Color.BLUE);
		// LinearGradient shader =new LinearGradient(0, 0, radus*2, radus*2, new
		// int[] {Color.BLUE, Color.WHITE}, null, Shader.TileMode.CLAMP);
		// paint.setShader(shader);

		// canvas.drawCircle(radus+paint_width, radus+paint_width, radus,paint);
		RectF rf = new RectF(paint_width, paint_width, (int) (radus * 2)
				+ paint_width, (int) (radus * 2) + paint_width);
		canvas.drawArc(rf, -90f, angle, false, paint);
		textpaint = new Paint();
		textpaint.setTextAlign(Paint.Align.CENTER);
		textpaint.setAntiAlias(true);
		textpaint.setTextSize(textSize);
		textpaint.setColor(outCircle);
//		canvas.drawText(text_angle + "%", paint_width + radus, paint_width
//				+ radus + textSize / 2, textpaint);
		canvas.drawText(text_angle, paint_width + radus, paint_width
				+ radus + textSize / 2, textpaint);
	}

	@Override
	public synchronized void run() {
		while (true) {
			angle = angle + 1f;
			angle=corner;
			postInvalidate();

		}
	}
}
